﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daemons_win
{
    public partial class mainForm : Form
    {

        private static string logPath = @"daemons_win_log.txt";

        public mainForm()
        {
            InitializeComponent();
        }


        private void mainForm_Load(object sender, EventArgs e)
        {
            state(false);
            TestingTimer.Interval = Convert.ToInt32(TimeNumericUpDown.Value);
        }

        private void state(bool value) {
            NameTextBox.Enabled = false;
            SelectButton.Enabled = !value;
            CmdTextBox.Enabled = !value;
            TimeNumericUpDown.Enabled = !value;
            LogCheckBox.Enabled = !value;

            if (value)
            {
                Text = "Daemons Start....";
            }
            else {

                Text = "Daemons";
            }

            writeLog("log",Text);

        }

        private void StartCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (StartCheckBox.Checked)
            {
                TestingTimer.Start();
            }
            else {
                TestingTimer.Stop();
            }
            if (NameTextBox.Text == "") {
                StartCheckBox.Checked = false;
                return;
            }
            state(StartCheckBox.Checked);
        }

        private void TestingTimer_Tick(object sender, EventArgs e)
        {
            if (!isProcess(Path.GetFileNameWithoutExtension(NameTextBox.Text))) {
                try
                {
                    using (Process pr = new Process())
                    {
                        pr.StartInfo.FileName = NameTextBox.Text;
                        pr.StartInfo.Arguments = CmdTextBox.Text;
                        pr.Start();
                    }
                    writeLog("log" ,NameTextBox.Text + "    START OK");
                }
                catch
                {
                    writeLog("erro" ,NameTextBox.Text + "    START ERRO");
                }
            }
        }

        private void SelectButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Title = "选择进程";
            openfile.Filter = "可执行文件|*.exe";
            if (DialogResult.OK == openfile.ShowDialog())
            {
                NameTextBox.Text = openfile.FileName;
            }
        }

        private void writeLog(string logType, string valueStr) {
            if (!LogCheckBox.Checked) return;
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(logPath, true))
            {
                file.WriteLine( "["+ logType + "]  "+ "[" + DateTime.Now.ToLocalTime().ToString() + "]    " + valueStr);
            }
        }

        private bool isProcess(string name) {

            Process[] processes = Process.GetProcesses();
            foreach (var item in processes) {
                if (name == item.ProcessName) return true;
            }
            return false;
        }
    }
}
